package com.lm.atomikos.service;

import com.lm.atomikos.mapper.order.OrderMapper;
import com.lm.atomikos.mapper.storage.StorageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class BusinessService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private StorageMapper storageMapper;

    @Transactional
    public void createOrder(String productId, int count) {
        orderMapper.insert(productId, count);
        storageMapper.decrease(productId, count);

        if (count > 5) {
            throw new RuntimeException("模拟异常，触发分布式事务回滚");
        }
    }
}
